﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Angle.Krill.Utils
{
    public static class EnumExt
    {
        /// <summary>
        /// 获取枚举的描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            return value.GetType()
                .GetMember(value.ToString())
                .FirstOrDefault()?
                .GetCustomAttribute<DescriptionAttribute>()?
                .Description;
        }
        /// <summary>
        /// 转枚举
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ToEnum<T>(this string eStr) where T : Enum
        {
            if (string.IsNullOrEmpty(eStr))
            {
                return default;
            }

            T t = (T)Enum.Parse(typeof(T), eStr, true);

            return t;
        }
    }
}
